﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.User;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Ekton_Manage_Web.Controllers
{
    /// <summary>
    /// 消息推送
    /// </summary>
    /// 
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class NewsController : ControllerBase
    {
        private readonly IBaseRepository<msg_push, int> _repository;
        private readonly ISelectMsgPushList _selectMsgPushList;
    

       public NewsController(IBaseRepository<msg_push, int> repository, ISelectMsgPushList selectMsgPushList)
        {
            _repository = repository;
            _selectMsgPushList = selectMsgPushList;
        }

        /// <summary>
        /// 查询全部消息推送
        /// </summary>
        /// 
        [HttpGet]
        public async Task<MessageModel<PageModel<msg_push>>> GetAllNewsList([FromQuery] QueryDataParame query)
        {
            var result = await _selectMsgPushList.SelectMsgList(query);

            return new MessageModel<PageModel<msg_push>>
            {
                msg = "数据获取成功",
                response = new PageModel<msg_push>
                {
                    pageIndex = query.pageIndex,
                    pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(result.Item2) / query.pageSize)),
                    dataCount = result.Item2,
                    pageSize = query.pageSize,
                    data = result.Item1
                }
            };
        }


        /// <summary>
        /// 编辑消息推送
        /// </summary>
        /// 
        [HttpPut]
        public async Task<Message> PutNewsInfo([FromBody] DtoInsertMsgPush query)
        {
            return await _selectMsgPushList.PutMsgList(query);
        }

        /// <summary>
        /// 添加消息推送
        /// </summary>
        /// <remarks>
        /// push 为推送方式 0：系统推送 1：邮箱推送
        /// push_method 0:立即推送 1：定时推送
        /// push_user为0代表推送全部用户，为1代表推送指定用户，需要传push_user_list   例子："1,2,3"
        /// state为推送状态（已推送/未推送）
        /// push_name为推送人
        /// idcident_name为推送事件名
        /// </remarks>
        /// 
        [HttpPost]
        public async Task<Message> PostNewsInfo([FromBody] DtoInsertMsgPush query)
        {
            return await _selectMsgPushList.PostMsgList(query);
        }

        /// <summary>
        /// 定时推送消息 点击推送后立即推送
        /// </summary>
        /// <remarks>
        /// 传消息推送的id
        /// </remarks>
        /// <returns></returns>
        [HttpPut]
        public async Task<Message> PutMsgListByNow(int id)
        {
            return await _selectMsgPushList.PutMsgListByNow(id);
        }

        /// <summary>
        /// 删除消息推送
        /// </summary>
        /// 
        [HttpDelete("{id}")]
        public async Task<Message> DeleteNewsInfo(int id)
        {
            return await _selectMsgPushList.DeleteMsgList(id);
        }
    }
}
